package com.batchplatform.module.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.batchplatform.common.entity.module.Module;
import com.batchplatform.module.service.ModuleService;
import com.batchplatform.system.service.SysManageService;

@Controller
public class ModuleController {
	@Autowired
	private ModuleService service;
	@Autowired
	private SysManageService sysService;
	
	@RequestMapping("/displayModules.do")
	public String displayModules(Model model) {
		List<Module> moduleList = service.getModules();
//		for(Module module : moduleList) {
//			System.out.println("module.getSystem().getName() = "+module.getSystem().getName());
//		}
		model.addAttribute("moduleList",moduleList);
		model.addAttribute("systemList", sysService.getSystems());
//		map.addAttribute("systemIdNames", sysService.getSystemIdNames());
		return "module/manage/modules";
	}
	
	@RequestMapping("/toAddModule")
    public String toAdd(Model model){
		model.addAttribute("systemList", sysService.getSystems());
        return "module/manage/addModule";
    }
	
	@RequestMapping("/saveModule.do")
	public String saveModule(Module module,
//						   @RequestParam("system_id")int system_id,
						   Model model) {
//		System system = sysService.getSystem(system_id);		
//		if(id == null) {
//			service.addModule(name, desc,system);
//		} else {
//			service.updateModule(Integer.parseInt(id), name, desc,system);
//		}
		service.addModule(module);
		return displayModules(model);
	}
	
	@RequestMapping("/toUpdateModule")
    public String toUpdate(int id, Model model){
		Module module = service.getModule(id);
        model.addAttribute("module",module);
        model.addAttribute("systemList", sysService.getSystems());
        return "module/manage/updateModule";
    }
	
	@RequestMapping("/updateModule.do")
	public String updateModule(Module module, Model model) {
//		model.addAttribute("module", service.getModule(id));
//		model.addAttribute("systemList", sysService.getSystems());
//		return "system/module/updateModuleForm";
		
		service.updateModule(module);
		return displayModules(model);
		
	}
	
	@RequestMapping("/deleteModule")
	public String deleteModule(@RequestParam("id") int id, Model model) {
		service.deleteModule(id);
		return displayModules(model);
	}
	
}

